package com.tanxi521.tnlevel.command;

import com.tanxi521.tnlevel.TnLevel;
import com.tanxi521.tnlevel.dao.ConnectionPool;
import com.tanxi521.tnlevel.dao.SQLManager;
import com.tanxi521.tnlevel.pojo.PlayerLevel;
import com.tanxi521.tnlevel.utils.BaseUtil;
import com.tanxi521.tnlevel.utils.ConfigReader;
import com.tanxi521.tnlevel.utils.LevelCounter;
import com.tanxi521.tnlevel.utils.TopPageHelper;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * {@code @Author:} i-xiaotian
 * {@code @Date:} 2022/3/13 15:12
 * {@code @EMAIL:} 1254806967@qq.com
 */
public class TnlevelCommands implements CommandExecutor, TabCompleter {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (args.length == 0 || (args.length == 1 && args[0].equalsIgnoreCase("help"))) {
            if (sender.isOp()) {
                sender.sendMessage("§2================[等级系统]================");
                sender.sendMessage("/tnlevel me                       查看自己的等级信息");
                sender.sendMessage("/tnlevel setlevel [玩家id] [等级]  设置玩家等级");
                sender.sendMessage("/tnlevel givelevel [玩家id] [等级]  给予玩家等级");
                sender.sendMessage("/tnlevel givexp [玩家id] [经验]    给予玩家经验值");
                sender.sendMessage("/tnlevel takexp [玩家id] [经验]    扣除玩家经验值");
                sender.sendMessage("/tnlevel info [玩家id]            查看玩家等级信息");
                sender.sendMessage("/tnlevel top [玩家id]            查看服务器等级排名");
                sender.sendMessage("/tnlevel reload                   重载插件");
                sender.sendMessage("§2================[等级系统]================");
                return true;
            } else {
                sender.sendMessage("§2================[等级系统]================");
                sender.sendMessage("/tnlevel me                     查看自己的等级信息");
                sender.sendMessage("/tnlevel info [玩家id]           查看玩家等级信息");
                sender.sendMessage("/tnlevel top [玩家id]            查看服务器等级排名");
                sender.sendMessage("§2================[等级系统]================");
            }
        }
        if (args.length == 1) {
            if (args[0].equalsIgnoreCase("setLevel")) sender.sendMessage("/tnlevel setlevel [玩家id] [等级]  设置玩家等级");
            if (args[0].equalsIgnoreCase("setLevel")) sender.sendMessage("/tnlevel givelevel [玩家id] [等级]  给予玩家等级");
            if (args[0].equalsIgnoreCase("giveXp")) sender.sendMessage("/tnlevel givexp [玩家id] [经验]    给予玩家经验值");
            if (args[0].equalsIgnoreCase("takeXp")) sender.sendMessage("/tnlevel takexp [玩家id] [经验]    扣除玩家经验值");
            if (args[0].equalsIgnoreCase("info")) sender.sendMessage("/tnlevel info [玩家id]            查看玩家等级信息");
            if (args[0].equalsIgnoreCase("reload") && sender.isOp()) {
                TnLevel.getInstance().reloadConfig();
                LevelCounter.initCounter();
                ConnectionPool.close(ConnectionPool.pool);
                if (ConfigReader.getMysqlUse()) ConnectionPool.initPoolMysql();
                else ConnectionPool.initPoolSQLITE();
                sender.sendMessage("§2[等级系统]§e重载成功");
            }
            if (args[0].equalsIgnoreCase("me")) {
                try {
                    Player player = (Player) sender;
                    PlayerLevel playerLevel = LevelCounter.getCache(player.getUniqueId().toString());
                    if (playerLevel.getPlayerLevel() == null) throw new Exception();
                    BaseUtil.printPlayerLevel(sender, playerLevel);
                } catch (Exception e) {
                    sender.sendMessage("§c[等级系统]玩家不存在");
                }
            }
            if (args[0].equalsIgnoreCase("top")) {
                TopPageHelper.topPage(1, sender);
            }
            return true;
        }
        if (args.length == 2) {
            if (args[0].equalsIgnoreCase("info")) {
                try {
                    PlayerLevel playerLevel = SQLManager.gerPlayerLevelByName(args[1]);
                    if (playerLevel.getPlayerLevel() == null) throw new Exception();
                    BaseUtil.printPlayerLevel(sender, playerLevel);
                    return true;
                } catch (Exception e) {
                    sender.sendMessage("§c[等级系统]玩家不存在");
                }
                return true;
            }
            if (args[0].equalsIgnoreCase("top")) {
                TopPageHelper.topPage(Integer.parseInt(args[1]), sender);
                return true;
            }
            return false;
        }
        if (args.length == 3) {
            if (args[0].equalsIgnoreCase("setLevel") && sender.isOp()) {
                try {
                    Player player = Bukkit.getServer().getPlayer(args[1]);
                    LevelCounter.countPlayerLevelByLevel(args[1], Integer.parseInt(args[2]), player);
                } catch (Exception e) {
                    sender.sendMessage("§c[等级系统]玩家不在线");
                    return false;
                }
                sender.sendMessage("§e[等级系统]设置玩家 " + args[1] + " 等级为 " + args[2]);
                Objects.requireNonNull(TnLevel.getInstance().getServer()
                        .getPlayer(args[1])).sendMessage("§e[等级系统]管理员设置你的等级为" + args[2]);
                return true;
            }
            if (args[0].equalsIgnoreCase("giveLevel") && sender.isOp()) {
                try {
                    Player player = Bukkit.getServer().getPlayer(args[1]);
                    PlayerLevel playerLevel = SQLManager.gerPlayerLevelByName(args[1]);
                    int levelCount = Integer.parseInt(args[2]) + playerLevel.getPlayerLevel();
                    LevelCounter.countPlayerLevelByLevel(args[1], levelCount, player);
                } catch (Exception e) {
                    sender.sendMessage("§c[等级系统]玩家不在线");
                    return false;
                }
                sender.sendMessage("§e[等级系统]给予玩家 " + args[1] + " " + args[2] + " 等级");
                Objects.requireNonNull(TnLevel.getInstance().getServer()
                        .getPlayer(args[1])).sendMessage("§e[等级系统]管理员给予你 " + args[2] + " 等级");
                return true;
            }
            if (args[0].equalsIgnoreCase("giveXp") && sender.isOp()) {
                Player player = Objects.requireNonNull(TnLevel.getInstance().getServer()
                        .getPlayer(args[1])).getPlayer();
                try {
                    assert player != null;
                    LevelCounter.countPlayerLevelByExp(player, Integer.parseInt(args[2]));
                    if (TnLevel.getInstance().getServer()
                            .getPlayer(args[1]) != null) {
                        player.playSound(player.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1F, 0F);
                    }

                } catch (Exception e) {
                    sender.sendMessage("§c[等级系统]玩家不在线");
                    return false;
                }
                sender.sendMessage("§e[等级系统]给予玩家 " + args[1] + " 经验为 " + args[2] + " 点");
                Objects.requireNonNull(TnLevel.getInstance().getServer()
                        .getPlayer(args[1])).sendMessage("§e[等级系统]管理员给予你" + args[2] + " 点经验");
                return true;
            }
            if (args[0].equalsIgnoreCase("takeXp") && sender.isOp()) {
                try {
                    LevelCounter.countPlayerLevelByExp(Objects.requireNonNull(TnLevel.getInstance().getServer()
                            .getPlayer(args[1])), -Integer.parseInt(args[2]));
                    Objects.requireNonNull(TnLevel.getInstance().getServer().getPlayer(args[1])).
                            playSound(Objects.requireNonNull(TnLevel.getInstance().getServer().getPlayer(args[1]))
                                    .getLocation(), Sound.ENTITY_EXPERIENCE_BOTTLE_THROW, 1F, 0F);
                } catch (Exception e) {
                    sender.sendMessage("§c[等级系统]玩家不在线");
                    return false;
                }
                sender.sendMessage("§e[等级系统]扣除玩家 " + args[1] + " 经验为 " + args[2] + " 点");
                Objects.requireNonNull(TnLevel.getInstance().getServer()
                        .getPlayer(args[1])).sendMessage("§e[等级系统]管理员扣除你" + args[2] + " 点经验");
                return true;
            }
        }
        return false;
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
        if (args.length == 1) {
            if (sender.isOp()) {
                String[] cmds = {"me", "top", "setlevel", "givelevel", "givexp", "takexp", "info", "reload"};
                return Arrays.stream(cmds).filter(s -> s.startsWith(args[0])).collect(Collectors.toList());
            } else {
                String[] cmds = {"me", "info", "top"};
                return Arrays.stream(cmds).filter(s -> s.startsWith(args[0])).collect(Collectors.toList());
            }
        }
        return null;
    }
}
